package com.grad.why.studentsystem.controller;

import com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSON;
import com.grad.why.studentsystem.domain.Classinfo;
import com.grad.why.studentsystem.domain.Course;
import com.grad.why.studentsystem.domain.Project;
import com.grad.why.studentsystem.domain.Student;
import com.grad.why.studentsystem.domain.current.CurrentCourse;
import com.grad.why.studentsystem.domain.current.CurrentCourseArrangeInfo;
import com.grad.why.studentsystem.domain.current.State;
import com.grad.why.studentsystem.domain.current.TreeNode;
import com.grad.why.studentsystem.service.ChooseCourseService;
import com.grad.why.studentsystem.service.ClassesTableService;
import com.grad.why.studentsystem.service.CourseArrangeService;
import com.grad.why.studentsystem.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/studentManageSystem")
public class ClassesTableController {
    @Autowired
    private ClassesTableService classesTableService;

    @Autowired
    private CourseArrangeService courseArrangeService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private ChooseCourseService chooseCourseService;

    @RequestMapping("/getStudentTreeViews")
    @ResponseBody
    public String getTreeNode(){
        try{
            TreeNode tree = new TreeNode();
            ArrayList<TreeNode> nodes = new ArrayList<TreeNode>();
            List<Project> projects = classesTableService.getProjects();
            //添加专业
            for(int i = 0;i<projects.size();i++){
                TreeNode projectnode = new TreeNode();
                projectnode.setText(projects.get(i).getName());
                List<TreeNode> classesNodes = new ArrayList<>();
                List<Classinfo> classes = classesTableService.getClassesByProjectId(projects.get(i).getId());
                //添加班级
                for(int j = 0;j<classes.size();j++){
                    TreeNode classnode = new TreeNode();
                    Classinfo classinfo = classes.get(j);
                    classnode.setText(classinfo.getClassname());
                    classnode.setState(new State());
                    List<TreeNode> studentnodes = new ArrayList<>();
                    List<Student> students = classesTableService.getStudentsInfoByClassId(classinfo.getId());
                    //添加学生
                    for(int k = 0;k<students.size();k++){
                        TreeNode studentnode = new TreeNode();
                        Student stu = students.get(k);
                        studentnode.setText(stu.getId()+"_"+stu.getName());
                        studentnode.setState(new State());
                        studentnodes.add(studentnode);
                    }
                    classnode.setNodes(studentnodes);
                    classesNodes.add(classnode);
                }
                projectnode.setNodes(classesNodes);
                State state = new State();
                projectnode.setState(state);
                nodes.add(projectnode);
            }
            tree.setText("专业目录");
            tree.setState(new State());
            tree.setNodes(nodes);
            String jsons = JSON.toJSON(tree).toString();
            return "["+jsons
                    .replaceAll("\"nodes\"","nodes")
                    .replaceAll("\"state\"","state")
                    .replaceAll("\"expanded\"","expanded")
                    .replaceAll("\"disabled\"","disabled")
                    .replaceAll("\"selected\"","selected")
                    .replaceAll("\"text\"","text")
                    .replaceAll("\"checked\"","checked")
                    +"]";

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/getStudentCourseTableBySid/{sid}")
    @ResponseBody
    public String getStudentCourseTableBySid(@PathVariable("sid") String sid){
        try{

            //获取学生所选的所有未结课的课程
            List<Course> courses = courseService.getStudentCourseTableBySid(Integer.parseInt(sid));
            List<List<CurrentCourseArrangeInfo>> courseArrangeInfos = new ArrayList<List<CurrentCourseArrangeInfo>>();

            for(int i = 0;i<courses.size();i++){
                //根据课程获取该课程的课程安排
                Course c = courses.get(i);
                List<CurrentCourseArrangeInfo> currentCourseArrangeInfos = courseArrangeService.getCourseArrangeInfoByCid(c.getCourseid());
                courseArrangeInfos.add(currentCourseArrangeInfos);
            }
            if(courseArrangeInfos.size() == 0){
                return null;
            }else{
                String[][] week = new String[5][10];
                for(int i = 0;i<week.length;i++){
                    for(int j = 0;j<week[0].length;j++){
                        week[i][j] = "";
                    }
                }

                for(int i = 0;i<courseArrangeInfos.size();i++){
                    List<CurrentCourseArrangeInfo> infos = courseArrangeInfos.get(i);
                    for(CurrentCourseArrangeInfo c : infos){
                        int[] indexs = getCourseIndex(c.getCourseindex());
                        int weekday = getWeekDayIndex(c.getWeekday());
                        if(weekday >= 6){
                            continue;
                        }
                        if(week[weekday][indexs[0]].equals("")){
                            week[weekday][indexs[0]] = "'"+c.getCoursename() + "@"+c.getClassroomname()+"@"+c.getTeachername()+"@"+c.getStartweek()+"周-"+c.getEndweek()+"周"+"'";
                        }else{
                            week[weekday][indexs[0]] = week[weekday][indexs[0]].replaceAll("'","");
                            week[weekday][indexs[0]] = "'"+week[weekday][indexs[0]] + "|" + c.getCoursename() + c.getClassroomname()+"@"+c.getTeachername()+"@"+c.getStartweek()+"周-"+c.getEndweek()+"周"+"'";
                        }

                        if(week[weekday][indexs[1]].equals("")){
                            week[weekday][indexs[1]] = "'"+c.getCoursename() + "@"+c.getClassroomname()+"@"+c.getTeachername()+"@"+c.getStartweek()+"周-"+c.getEndweek()+"周"+"'";
                        }else{
                            week[weekday][indexs[1]] = week[weekday][indexs[1]].replaceAll("'","");
                            week[weekday][indexs[1]] = "'"+week[weekday][indexs[1]]+"|"+c.getCoursename() + c.getClassroomname()+"@"+c.getTeachername()+"@"+c.getStartweek()+"周-"+c.getEndweek()+"周"+"'";
                        }
                    }
                }

                for(int i = 0;i<week.length;i++){
                    for(int j = 0;j<week[0].length;j++){
                        if(week[i][j].equals("")){
                            week[i][j] = "''";
                        }
                    }
                }

                StringBuffer sb = new StringBuffer("[");
                for(int i = 0;i<week.length;i++){
                    sb.append("[");
                    for(int j = 0;j<week[0].length;j++){
                        if(j<week[0].length-1){
                            sb.append(week[i][j] + ",");
                        }else{
                            sb.append(week[i][j]);
                        }
                    }
                    if(i<week.length-1) {
                        sb.append("],");
                    }else{
                        sb.append("]");
                    }
                }
                sb.append("]");
                return sb.toString();
            }
        }catch (Exception E){
            E.printStackTrace();
        }
        return null;
    }

    public int getWeekDayIndex(String weekday){
        String[] weekdays = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
        for(int i = 0;i<weekdays.length;i++){
            if(weekday.equals(weekdays[i])){
                return i;
            }
        }
        return -1;
    }

    public int[] getCourseIndex(String courseIndex){
        String[] Index = new String[]{"第一大节","第二大节","第三大节","第四大节","第五大节"};
        int[] indexs = new int[2];
        if(courseIndex.equals(Index[0])){
            indexs[0] = 0;
            indexs[1] = 1;
        }else if(courseIndex.equals(Index[1])){
            indexs[0] = 2;
            indexs[1] = 3;
        }else if(courseIndex.equals(Index[2])){
            indexs[0] = 4;
            indexs[1] = 5;
        }else if(courseIndex.equals(Index[3])){
            indexs[0] = 6;
            indexs[1] = 7;
        }else if(courseIndex.equals(Index[4])){
            indexs[0] = 8;
            indexs[1] = 9;
        }
        return indexs;
    }
}
